import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/layout/";

Vue.use(VueRouter);

export const constantRoutes = [
  {
    path: "/login",
    component: () => import(/* webpackChunkName: "about" */ "@/views/login")
  },

  {
    path: "/",
    name: "Home",
    component: Layout,
    children: [
      {
        // 当 /user/:id/profile 匹配成功，
        // UserProfile 会被渲染在 User 的 <router-view> 中
        path: "/",
        component: () =>
          import(/* webpackChunkName: "about" */ "@/views/Home.vue")
      }
    ]
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    name: "Page404",
    meta: { title: "404", noCache: true }
  },
  {
    path: "/about",
    name: "About",
    component: Layout,
    children: [
      {
        // 当 /user/:id/profile 匹配成功，
        // UserProfile 会被渲染在 User 的 <router-view> 中
        path: "/",
        component: () =>
          import(/* webpackChunkName: "about" */ "@/views/About.vue")
      }
    ]
  }
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: "/icon", //图标预览页面
    component: () => import("@/views/icons/index"),
    meta: {
      title: "Permission",
      icon: "lock",
      roles: ["admin"] // you can set roles in root nav
    }
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

// const router = new VueRouter({
//   routes
// });

const createRouter = () =>
  new VueRouter({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}
export default router;
